package cn.iocoder.yudao.module.handguard.service.productaccess;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.handguard.dal.dataobject.productaccessitem.ProductAccessItemDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.productaccesspercentage.ProductAccessPercentageDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.publicitytype.PublicityTypeDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.usertablesaudit.UserTablesAuditDO;
import cn.iocoder.yudao.module.handguard.dal.mysql.housingresource.HousingResourceMapper;
import cn.iocoder.yudao.module.handguard.dal.mysql.productaccessitem.ProductAccessItemMapper;
import cn.iocoder.yudao.module.handguard.dal.mysql.productaccesspercentage.ProductAccessPercentageMapper;
import cn.iocoder.yudao.module.handguard.dal.mysql.publicitytype.PublicityTypeMapper;
import com.alibaba.excel.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.admin.productaccess.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.productaccess.ProductAccessDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.productaccess.ProductAccessConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.productaccess.ProductAccessMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 产品准入 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ProductAccessServiceImpl implements ProductAccessService {

    @Resource
    private ProductAccessMapper productAccessMapper;

    @Resource
    private ProductAccessItemMapper productAccessItemMapper;

    @Resource
    private ProductAccessPercentageMapper productAccessPercentageMapper;

    @Resource
    private PublicityTypeMapper publicityTypeMapper;

    @Resource
    private HousingResourceMapper housingResourceMapper;

    @Override
    public Long createProductAccess(ProductAccessCreateReqVO createReqVO) {
        // 插入
        ProductAccessDO productAccess = ProductAccessConvert.INSTANCE.convert(createReqVO);
        if (createReqVO.getPublicityTypeId()==2 || createReqVO.getPublicityTypeId()==4 || createReqVO.getPublicityTypeId()==8
                ||  createReqVO.getPublicityTypeId()==9 || createReqVO.getPublicityTypeId()==10 || createReqVO.getPublicityTypeId()==11
                || createReqVO.getPublicityTypeId()==12 || createReqVO.getPublicityTypeId()==13){
            if (createReqVO.getMultiple()==null){
                throw exception(PRODUCT_MULTIPLE_NOT_EXISTS);
            }
        }

        if (createReqVO.getPublicityTypeId()==1 || createReqVO.getPublicityTypeId()==5 || createReqVO.getPublicityTypeId()==7 ||
                createReqVO.getPublicityTypeId()==8 || createReqVO.getPublicityTypeId()==9
                || createReqVO.getPublicityTypeId()==10 || createReqVO.getPublicityTypeId()==14
                || createReqVO.getPublicityTypeId()==15 || createReqVO.getPublicityTypeId()==16){
            if (createReqVO.getPortions()==null){
                throw exception(PRODUCT_PORTIONS_ONE_NOT_EXISTS);
            }
        }

//        if (createReqVO.getPublicityTypeId()!=10 && createReqVO.getPublicityTypeId()!=14){
//            if (StringUtils.isEmpty(createReqVO.getBank())){
//                throw exception(BANK_CHECK_NULL);
//            }
//        }

//        if (createReqVO.getPublicityTypeId()==7 ){
//            if (createReqVO.getPortionsTwo()==null){
//                throw exception(PRODUCT_PORTIONS_ONE_NOT_EXISTS);
//            }
//        }

        productAccessMapper.insert(productAccess);
        // 返回
        return productAccess.getId();
    }

    @Override
    public void updateProductAccess(ProductAccessUpdateReqVO updateReqVO) {
        // 校验存在
        validateProductAccessExists(updateReqVO.getId());

        if (updateReqVO.getPublicityTypeId()==2 || updateReqVO.getPublicityTypeId()==4 || updateReqVO.getPublicityTypeId()==8
                ||  updateReqVO.getPublicityTypeId()==9 || updateReqVO.getPublicityTypeId()==10  || updateReqVO.getPublicityTypeId()==11
                || updateReqVO.getPublicityTypeId()==12 ||  updateReqVO.getPublicityTypeId()==13){
            if (updateReqVO.getMultiple()==null){
                throw exception(PRODUCT_MULTIPLE_NOT_EXISTS);
            }
        }

        if (updateReqVO.getPublicityTypeId()==1 || updateReqVO.getPublicityTypeId()==5 || updateReqVO.getPublicityTypeId()==7 || updateReqVO.getPublicityTypeId()==8 || updateReqVO.getPublicityTypeId()==9
                || updateReqVO.getPublicityTypeId()==10 || updateReqVO.getPublicityTypeId()==14
                || updateReqVO.getPublicityTypeId()==15 || updateReqVO.getPublicityTypeId()==16){
            if (updateReqVO.getPortions()==null){
                throw exception(PRODUCT_PORTIONS_ONE_NOT_EXISTS);
            }
        }

//        if (updateReqVO.getPublicityTypeId()!=10 && updateReqVO.getPublicityTypeId()!=14){
//            if (StringUtils.isEmpty(updateReqVO.getBank())){
//                throw exception(BANK_CHECK_NULL);
//            }
//        }

//        if (updateReqVO.getPublicityTypeId()==7 ){
//            if (updateReqVO.getPortionsTwo()==null){
//                throw exception(PRODUCT_PORTIONS_ONE_NOT_EXISTS);
//            }
//        }

        //校验是否存在准入项，如果类型不一致则先清除之前的准入项
        ProductAccessDO accessDO =  productAccessMapper.selectById(updateReqVO.getId());
        if (updateReqVO.getPublicityTypeId() != accessDO.getPublicityTypeId()){
            productAccessItemMapper.deleteByPid(updateReqVO.getId());
        }

        // 更新
        ProductAccessDO updateObj = ProductAccessConvert.INSTANCE.convert(updateReqVO);
        productAccessMapper.updateById(updateObj);
    }

    @Override
    public void deleteProductAccess(Long id) {
        // 校验存在
        validateProductAccessExists(id);
        // 删除
        productAccessMapper.deleteById(id);
        //删除子项数据
        //productAccessItemMapper.delete(new LambdaUpdateWrapper<ProductAccessItemDO>().eq(ProductAccessItemDO::getProductAccessId,id));
    }

    private void validateProductAccessExists(Long id) {
        if (productAccessMapper.selectById(id) == null) {
            throw exception(PRODUCT_ACCESS_NOT_EXISTS);
        }
    }

    @Override
    public ProductAccessDO getProductAccess(Long id) {
        return productAccessMapper.selectById(id);
    }

    @Override
    public List<ProductAccessDO> getProductAccessList(Collection<Long> ids) {
        return productAccessMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProductAccessDO> getProductAccessPage(ProductAccessPageReqVO pageReqVO) {
        PageResult<ProductAccessDO> pageResult =  productAccessMapper.selectPage(pageReqVO);
        if (pageResult.getList().size()>0){
            for (ProductAccessDO p : pageResult.getList()){
                PublicityTypeDO typeDO = publicityTypeMapper.selectById(p.getPublicityTypeId());
                p.setTypeNames(typeDO.getTypeNames());
            }
        }
        return pageResult;
    }

    @Override
    public List<ProductAccessDO> getProductAccessList(ProductAccessExportReqVO exportReqVO) {
        return productAccessMapper.selectList(exportReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createCopyAccess(Long id) {
        //复制
        ProductAccessDO accessDO =   productAccessMapper.selectById(id);
        accessDO.setId(null);
        accessDO.setCreateTime(LocalDateTime.now());
        accessDO.setUpdateTime(LocalDateTime.now());

        if (!accessDO.getProductName().contains("-复制")){
            String name  = accessDO.getProductName() +"-复制";
            accessDO.setProductName(name);
        }
        productAccessMapper.insert(accessDO);
        //复制准入项
        List<ProductAccessItemDO> itemList = productAccessItemMapper.selectList(new LambdaQueryWrapper<ProductAccessItemDO>()
                .eq(ProductAccessItemDO::getProductAccessId, id));
        if (itemList.size()>0){
            for (ProductAccessItemDO item : itemList){
                item.setId(null);
                item.setProductAccessId(accessDO.getId());
                item.setCreateTime(LocalDateTime.now());
                item.setUpdateTime(LocalDateTime.now());
                productAccessItemMapper.insert(item);
            }
        }
        //复制利润明细
        ProductAccessPercentageDO percentageDO =    productAccessPercentageMapper.selectOne(new LambdaQueryWrapper<ProductAccessPercentageDO>().eq(ProductAccessPercentageDO::getProductAccessId, id));
        if (percentageDO!=null){
            percentageDO.setId(null);
            percentageDO.setProductAccessId(accessDO.getId());
            percentageDO.setCreateTime(LocalDateTime.now());
            percentageDO.setUpdateTime(LocalDateTime.now());
            productAccessPercentageMapper.insert(percentageDO);
        }

        return accessDO.getId();
    }

    @Override
    public String getConfigDataByKey(String key) {
        return housingResourceMapper.getSslURL(key);
    }

}
